package com.woniuxy.service.impl;

import com.woniuxy.dao.ManagerLoginDao;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.RbacPerm;
import com.woniuxy.entity.RbacRole;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.ManagerLoginService;
import com.woniuxy.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Author: ideaAdmin
 * TODO: Class
 * TODO:
 * Date: 2022/2/23:17:07
 * Description:
 */
@Service
public class ManagerLoginServiceImpl implements ManagerLoginService {

    @Autowired(required = false)
    ManagerLoginDao managerLoginDao;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ResponseEntity findManager(RbacManager rbacManager) {
        RbacManager manager = managerLoginDao.findManager(rbacManager);

        if(manager != null){
            String token = JWTUtil.sign(manager.getAccount(), manager.getPassword());
            manager.setToken(token);
            redisTemplate.opsForValue().set(token,manager);
            redisTemplate.expire(token,30, TimeUnit.MINUTES);
            return new ResponseEntity(200,"登录成功",manager);
        }else {
            return new ResponseEntity(500,"账户密码错误",null);
        }
    }

    @Override
    public String findPasswordByAccount(String username) {
        return managerLoginDao.findPasswordByAccount(username);
    }

    @Override
    public Set<RbacRole> findRoleByAccount(String username) {
        return managerLoginDao.findRoleByAccount(username);
    }

    @Override
    public Set<RbacPerm> findPermByAccount(String username) {
        return managerLoginDao.findPermByAccount(username);
    }
}
